package com.finalab.yueqiu.common;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * @author 莉安
 */
public class BeanValidator {
    private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();



    /**
     * 验证某个bean的参数
     *
     * @param object 被校验的参数
     */
    public static <T> boolean validate(T object,Class ... groups) {
        //获得验证器
        //执行验证
        Set<ConstraintViolation<T>> constraintViolations = validatorFactory.getValidator().validate(object, groups);
        //如果有验证信息，则将第一个取出来包装成异常返回
        if(constraintViolations != null && constraintViolations.size() > 0){
           return false;
        }
        return true;
    }
}
